package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.config.ExThreadExtetns;
import com.itheima.entity.ShoppingCart;
import com.itheima.entity.User;
import com.itheima.result.R;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session) {
        log.info("查看购物车...");
        User user = (User) session.getAttribute("user");
        Long id = user.getId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, id);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {

        User user = (User) session.getAttribute("user");
        Long id = user.getId();
        LambdaQueryWrapper<ShoppingCart> lqWrapper = new LambdaQueryWrapper<>();
        lqWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(lqWrapper);
        if (one == null) {
            shoppingCart.setUserId(id);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        Integer number = one.getNumber();
        one.setNumber(number + 1);
        shoppingCartService.updateById(one);
        return R.success(one);
    }

    @PostMapping("/sub")
    public R deleteShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setMealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        wrapper.eq(setMealId != null, ShoppingCart::getSetmealId, setMealId);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one.getNumber() != 1) {
            Integer number = one.getNumber();
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        shoppingCartService.removeById(one);
        return R.success(0);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, ExThreadExtetns.getGlobalThread());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}